package com.shop.dao;

import java.sql.Connection;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.shop.domain.Order;
import com.shop.domain.OrderItem;
import com.shop.domain.Product;
import com.shop.domain.User;
import com.shop.utils.JDBCUtils;

public class OrderDao {

	/**
	 * 生成订单，多表插入
	 * 
	 * @param order
	 */
	public void addOrder(Order order) {
		Connection conn = null;
		// 添加订单，在admin用户中显示的所用订单
		String sql = "insert into orders values(?,?,?,null,0,?)";
		String orderID = (String) JDBCUtils.getOrderID();
		try {
			conn = JDBCUtils.getConnection();
			conn.setAutoCommit(false);
			Object[] params = { orderID, order.getTotalmoney(), order.getReceiverinfo(), order.getUser_id() };
			QueryRunner queryRunner = new QueryRunner();
			queryRunner.update(conn, sql, params);

			// 生成订单项，在admin用户中显示的订单中的所有商品量
			List<OrderItem> items = order.getOrderItems();
			String sql2 = "insert into orderitem values(?,?,?,?)";
			for (OrderItem item : items) {
				Object[] p = { orderID, item.getProduct_id(), item.getBuynum(), item.getMoney() };
				QueryRunner queryRunner2 = new QueryRunner();
				queryRunner2.update(conn, sql2, p);
			}
			DbUtils.commitAndCloseQuietly(conn);
		} catch (SQLException e) {
			DbUtils.rollbackAndCloseQuietly(conn);
			e.printStackTrace();
			throw new RuntimeException("订单生成失败");
		}
	}

	public List<Order> findAll() {
		String sql = "select * from orders";
		try {
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			List<Order> orders = queryRunner.query(sql, new BeanListHandler<Order>(Order.class));
			for(Order order : orders){
				User user = new UserDao().getUser(order.getUser_id());
				order.setUser(user);
			}
			return orders;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<OrderItem> findOrderItems(Order order) {
		String sql = "select * from orderitem where order_id = ?";
		try {
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			List<OrderItem> items = queryRunner.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),order.getId());
			for(OrderItem item : items){
				Product product = new ProductDao().findProduct(item.getProduct_id());
				item.setProduct(product);
			}
			return items;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Order> findByUser(User user) {
		String sql = "select * from orders where user_id = ?";
		try {
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			List<Order> orders = queryRunner.query(sql, new BeanListHandler<Order>(Order.class), user.getId());
			for(Order order : orders){
				order.setUser(user);
			}
			return orders;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Order findOrderByID(String id) {
		String sql = "select * from orders where id = ?";
		try {
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			Order order = queryRunner.query(sql, new BeanHandler<Order>(Order.class), id);
			return order;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void delOrderByID(String id) {
		Connection conn = null;
		try {
			conn = JDBCUtils.getConnection();
			QueryRunner queryRunner = new QueryRunner();
			conn.setAutoCommit(false);
			String sql1 = "delete from orderitem where order_id = ?";
			queryRunner.update(conn, sql1, id);
			
			String sql2 = "delete from orders where id = ?";
			queryRunner.update(conn, sql2, id);
			DbUtils.commitAndCloseQuietly(conn);
		} catch (SQLException e) {
			DbUtils.rollbackAndCloseQuietly(conn);
			e.printStackTrace();
			throw new RuntimeException("取消订单失败");
		}
		
	}

	public void updateState(String r6_Order) {
		String sql = "update  orders set state = 1 where id = ?";
		try {
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			queryRunner.update(sql, r6_Order);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
